import java.text.SimpleDateFormat
import java.util.regex.Pattern
import java.util.{Date, Locale}

import com.itstaredu.IpParser
import org.apache.spark.rdd.RDD

/**
  *  用于字符分割处理数据的程序
  */
  class MapUtilDemo {
    // 识别apache 日志+cookie 的正则表达式
    val APACHE_LOG_REGIX = ""
    // 识别不要带日志的cookie的正则
    val CANNOT_GET = "nothing"

    // 日志属性
    // IP地址
    var ipAddress = CANNOT_GET
    // 日期
    var dateTime = CANNOT_GET
    // 请求URL
    var requests = CANNOT_GET
    // 请求的状态
    var responseStats = CANNOT_GET
    // 发送的字节数
    var bytesSent =CANNOT_GET
    //  转发的URL
    var referer = CANNOT_GET
    //  浏览器类型
    var browser = CANNOT_GET
    //  区域地址
    var areaAddress =CANNOT_GET
    //  本地的地址
    var localAddress = CANNOT_GET
    //  接受的时间
    var receiveTime = CANNOT_GET

    def map(lines : RDD[String])=lines.map(f = sline =>{
        val line =sline.replace("bbs.moonseo.cn","")
        val pattern = Pattern.compile(APACHE_LOG_REGIX)
        val matcher = pattern.matcher(line)
        // 定义ip receiveTime url referUrl userAgent 变量
        var ipStr: String =null
        // 正则匹配
          if(matcher.matches()){
              ipAddress = matcher.group(1)
              dateTime = matcher.group(4)
              requests = matcher.group(5)
              responseStats = matcher.group(6)
              bytesSent = matcher.group(7)
              ipStr = ipAddress
              if(!matcher.group(8).equals("-")){
                  referer = matcher.group(8)
              }
              browser = matcher.group(9)
              val ipParser = new IpParser
              try{
                areaAddress = ipParser.parse(ipStr).split(" ")(0)
                localAddress = ipParser.parse(ipStr).split(" ")(1)
              }catch {
                case  e : Exception => e.printStackTrace()
              }
              val df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z",Locale.US)
              try{
                val date:Date = df.parse(dateTime)
                // 获取指定
                receiveTime = date.getTime.toString
              }catch {
                case e : Exception => e.printStackTrace()
              }
              ipAddress + "\t" + dateTime + "\t" + requests + "\t" + responseStats +"\t"  +  bytesSent +
                "\t"   + referer +"\t" + browser +"\t" + areaAddress +"\t" +
                "\t" + localAddress +"\t" + receiveTime
          }
    }

    )

}
